package se.slackers.floatingtableheader.client.generator;

import com.google.gwt.dom.client.Node;
import com.google.gwt.user.client.Element;

public class THEADHeaderGenerator extends AbstractHeaderGenerator {
	@Override
	public Element create(final Element table) {
		return recursiveClone(table);
	}

	private Element recursiveClone(final Element root) {
		final Element clonedRoot = (Element) root.cloneNode(false);
		for (int i = 0; i < root.getChildCount(); i++) {
			final Node child = root.getChild(i);
			if (child.getNodeType() != Node.ELEMENT_NODE) {
				clonedRoot.appendChild(child.cloneNode(true));
			} else {
				final String name = child.getNodeName();
				if (isTHEAD(name)) {
					clonedRoot.appendChild(cloneWithSize((Element) child));
				}
			}
		}
		return clonedRoot;
	}
}
